﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Basic_sprite_code
{
    class Goblin:Sprite
    {
        public Apprentice summoner;
        public Hero target;

        public bool guard;
        public bool slain;
        public int initX;
        public int animCounter;
        public float deathChance;

        public Texture2D pic1;
        public Texture2D pic2;
        public Texture2D picSlain;

        public Goblin(DarkLordsApprentice g, Apprentice a, Vector2 position, bool isGuard, int magicBoost)
            : base(g, "GoblinL1", position, new Vector2(50, 50))
        {
            summoner = a;
            guard = isGuard;
            slain = false;
            initX = (int) position.X;
            animCounter = 0;
            deathChance = 0.25f - 0.25f * (magicBoost / 100f);

            scale = 0.05f;

            pic1 = game.Content.Load<Texture2D>("GoblinL1");
            pic2 = game.Content.Load<Texture2D>("GoblinL2");
            picSlain = game.Content.Load<Texture2D>("GoblinSlain");
        }

        public override void Update(GameTime gameTime)
        {
            if (!game.gamePaused)
            {
                if (slain)
                {
                    switch (animCounter)
                    {
                        case 0:
                            image = picSlain;
                            animCounter++;
                            break;
                        case 40:
                            game.Components.Remove(this);
                            summoner.currGoblins--;
                            break;
                        default:
                            animCounter++;
                            break;
                    }
                }
                else
                {
                    switch (animCounter)
                    {
                        case 0:
                            image = pic1;
                            animCounter++;
                            break;
                        case 20:
                            image = pic2;
                            animCounter++;
                            break;
                        case 40:
                            animCounter = 0;
                            break;
                        default:
                            animCounter++;
                            break;
                    }



                    if (guard)
                    {
                        scale = 1.2f;
                        if (position.X > initX)
                        {
                            position.X -= 1;
                        }
                        else if (position.X < initX)
                        {
                            position.X += 1;
                        }
                    }
                    else
                    {
                        Vector2 currOffset = new Vector2(1000, 1000);
                        if (target == null)
                        {
                            Vector2 smallestOffset = new Vector2(1000, 1000);

                            foreach (Sprite s in AllSprites)
                            {
                                if (s is Hero)
                                {
                                    Hero h = (Hero)s;
                                    currOffset = h.position - this.position;
                                    if (currOffset.Length() < smallestOffset.Length() && h.position.Y < 450 && !h.fireDeath && !h.goblinDeath)
                                    {
                                        target = h;
                                        smallestOffset = currOffset;
                                    }
                                }
                            }
                        }
                        else
                        {
                            float posScale = (position.Y - 150.0f) / 300.0f;

                            if (target.position.Y > 450 || target.fireDeath || target.goblinDeath)
                            {
                                target = null;
                            }
                            else
                            {
                                currOffset = target.position - this.position;

                                if (currOffset.X > 0)
                                {
                                    pic1 = game.Content.Load<Texture2D>("GoblinR1");
                                    pic2 = game.Content.Load<Texture2D>("GoblinR2");
                                    picSlain = game.Content.Load<Texture2D>("GoblinSlainR");
                                }
                                else
                                {
                                    pic1 = game.Content.Load<Texture2D>("GoblinL1");
                                    pic2 = game.Content.Load<Texture2D>("GoblinL2");
                                    picSlain = game.Content.Load<Texture2D>("GoblinSlain");

                                }

                                this.position.X += currOffset.X / currOffset.Length();
                                this.position.Y += currOffset.Y / currOffset.Length();
                                scale = 0.25f + posScale * 0.75f;
                            }
                        }
                    }
                }

                base.Update(gameTime);
            }
        }
    }
}
